﻿using System.ComponentModel;
using System.Security.Cryptography;
using System.Windows;

namespace VNet.VisiSuite.Controls
{
    public class VNetMenuItem : INotifyPropertyChanged
    {
        #region Implement INotifiedPropertyChanged

        public event PropertyChangedEventHandler PropertyChanged;

        protected virtual void OnPropertyChanged(string propertyName)
        {
            if (PropertyChanged != null)
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }

        #endregion

        public delegate void OnSelection();

        private readonly string _background;
        private readonly string _foreground;
        private readonly string _highlight;
        private readonly string _icon;
        private readonly string _title;
        private readonly OnSelection _callback;
        private string _tooltip;
        private string _spacing;
        private bool _enabled;
        private Visibility _button_visibility;
        private string _padding;

        public VNetMenuItem(string title, string tooltip, string icon, VNetMenuItemType type, OnSelection callback, string spacing = "10", Visibility buttonVisibility = Visibility.Visible, string padding = "0")
        {
            _title = title;
            _icon = icon;
            Tooltip = tooltip;
            _callback = callback;
            _spacing = spacing;
            IsEnabled = true;
            _button_visibility = buttonVisibility;
            _padding = padding;

            switch (type)
            {
                case VNetMenuItemType.WhiteBackgroundTheme:
                    _foreground = "#FF00AADE";
                    _background = "White";
                    _highlight = "#FFB2E1EF";
                    break;
                case VNetMenuItemType.WhiteBackgroundBlackIconTheme:
                    _foreground = "Black";
                    _background = "White";
                    _highlight = "#FFB2E1EF";
                    break;
                case VNetMenuItemType.BlackBackgroundTheme:
                    _foreground = "White";
                    _background = "Black";
                    _highlight = "#FFB7B7B7";
                    break;
                case VNetMenuItemType.BlueWhiteTheme:
                    _foreground = "White";
                    _background = "#052569";
                    _highlight = "Black";
                    break;
                case VNetMenuItemType.RedWhiteTheme:
                    _foreground = "White";
                    _background = "Red";
                    _highlight = "Black";
                    break;
                case VNetMenuItemType.GreenWhiteTheme:
                    _foreground = "White";
                    _background = "Green";
                    _highlight = "Black";
                    break;
                case VNetMenuItemType.GrayWhiteTheme:
                    _foreground = "White";
                    _background = "Gray";
                    _highlight = "Black";
                    break;
                case VNetMenuItemType.YellowBlackTheme:
                    _foreground = "Black";
                    _background = "Yellow";
                    _highlight = "Black";
                    break;

            }
        }

        public string Padding
        {
            get { return _padding; }
        }

        public Visibility ButtonVisibility
        {
            get { return _button_visibility; }
        }

        public string Title
        {
            get { return _title; }
        }

        public Visibility TitleVisibility
        {
            get { return _title == null ? Visibility.Collapsed : Visibility.Visible; }
        }

        public string Spacing
        {
            get { return _spacing; }
        }

        public DataTemplate Icon
        {
            get
            {
                var res = (DataTemplate)VNetUserControl.FindResource(_icon);
                return res;
                //return res.LoadContent();
            }
        }

        public string IconKey
        {
            get { return _icon; }
        }

        public string Tooltip
        {
            get { return _tooltip; }
            set
            {
                if (_tooltip == value) return;
                _tooltip = value;
                OnPropertyChanged("Tooltip");
                OnPropertyChanged("ShowTooltip");
            }
        }

        public bool ShowTooltip
        {
            get { return !string.IsNullOrEmpty(_tooltip); }
        }

        public string Foreground
        {
            get { return _foreground; }
        }

        public string Background
        {
            get { return _background; }
        }

        // Mouse over
        public string HoverColor
        {
            get { return _enabled ? _highlight : _background; }
        }

        public void Selected()
        {
            if (_callback != null)
                _callback();
        }

        public bool IsEnabled
        {
            get { return _enabled; }
            set
            {
                if (_enabled == value) return;
                _enabled = value;
                OnPropertyChanged("IsEnabled");
            }
        }

        public double Opacity
        {
            get { return _enabled ? 1.0 : 0.5; }
        }
    }

    public enum VNetMenuItemType
    {
        WhiteBackgroundTheme,
        WhiteBackgroundBlackIconTheme,
        BlackBackgroundTheme,
        GreenWhiteTheme,
        RedWhiteTheme,
        BlueWhiteTheme,
        YellowBlackTheme,
        GrayWhiteTheme
    }
}